<!-- 物料选择器组件 -->
<template>
  <el-dialog
    title="选择物料"
    :visible.sync="dialogVisible"
    width="1000px"
    append-to-body
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <!-- 查询条件区域 -->
    <div class="filter-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
        <el-form-item label="物料编码" prop="materialCode">
          <el-input
            v-model="queryParams.materialCode"
            placeholder="请输入物料编码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input
            v-model="queryParams.materialName"
            placeholder="请输入物料名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="规格型号" prop="materialSfn">
          <el-input
            v-model="queryParams.materialSfn"
            placeholder="请输入规格型号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 物料表格 -->
    <el-table
      v-loading="loading"
      :data="materialList"
      border
      highlight-current-row
      @row-click="handleRowClick"
      style="width: 100%"
    >
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column label="物料ID" align="center" prop="material_id" width="80" :show-overflow-tooltip="true" />
      <el-table-column label="物料编码" align="center" prop="material_code" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="物料名称" align="center" prop="material_name" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="material_sfn" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="单位" align="center" prop="unit" width="80" />
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click.stop="handleSelect(scope.row)"
          >选择</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 底部操作区 -->
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleConfirm" :disabled="!selectedMaterial">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getQcTemplateProductList } from "@/api/qc/IncomingInspection";

export default {
  name: "MaterialSelector",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    templateId: {
      type: [String, Number],
      default: ""
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 物料列表
      materialList: [],
      // 当前选中的物料
      selectedMaterial: null,
      // 显示状态
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateId: "",
        materialCode: null,
        materialName: null,
        materialSfn: null
      }
    };
  },
  watch: {
    visible: {
      handler(val) {
        this.dialogVisible = val;
        if (val) {
          // 打开对话框时，将父组件传递的templateId设置到查询参数中
          this.queryParams.templateId = this.templateId || "";
          this.getList();
        }
      },
      immediate: true
    },
    templateId: {
      handler(val) {
        if (val) {
          this.queryParams.templateId = val;
        }
      },
      immediate: true
    }
  },
  methods: {
    // 获取物料列表
    getList() {
      this.loading = true;
      getQcTemplateProductList(this.queryParams.templateId,this.queryParams.materialCode,this.queryParams.materialName)
        .then(response => {
          if (response && response.code === 200) {
            this.materialList = response.rows || [];
            this.total = response.total || 0;
          } else {
            this.materialList = [];
            this.total = 0;
            this.$message.error(response?.msg || "获取物料列表失败");
          }
        })
        .catch(error => {
          console.error("获取物料列表失败", error);
          this.materialList = [];
          this.total = 0;
          this.$message.error("获取物料列表失败：" + (error.message || "未知错误"));
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 查询按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    // 行点击事件
    handleRowClick(row) {
      this.selectedMaterial = row;
    },
    // 选择按钮操作
    handleSelect(row) {
      this.selectedMaterial = row;
      this.handleConfirm();
    },
    // 确定按钮操作
    handleConfirm() {
      if (this.selectedMaterial) {
        this.$emit("select", this.selectedMaterial);
        this.handleClose();
      } else {
        this.$message.warning("请先选择一个物料");
      }
    },
    // 关闭对话框
    handleClose() {
      this.selectedMaterial = null;
      this.$emit("close");
      this.$emit("update:visible", false);
    }
  }
};
</script>

<style scoped>
.filter-container {
  margin-bottom: 15px;
}

.dialog-footer {
  padding-top: 20px;
}
</style> 